// 从 itemElement 中抽离出所有的 imgElements
export const getImgElements = (itemElements) => {
    const imgElements = []
    itemElements.forEach((element) => {
        imgElements.push(...element.getElementsByTagName('img'))
    })
    return imgElements
}

//生成所有的图片链接数组
export const getAllImg = (imgElements) => {
    return imgElements.map((ele) => {
        return ele.src
    })
}

//监听图片数组加载完成
export const onComplateImgs = (imgs) => {
    //promise异步合集
    const promiseAll = []

    //循环imgs， 构建promiseAll
    imgs.forEach((img, index) => {
        promiseAll[index] = new Promise((resolve, reject) => {
            //处理img的加载情况
            const imageObj = new Image()
            imageObj.src = img
            imageObj.onload = () => {
                resolve({
                    img,
                    index
                })
            }
            imageObj.onerror = () => {
                resolve({
                    img,
                    index
                })
            }
        })
    })
    return Promise.all(promiseAll)
}

//返回列高对象中最大的高度
export const getMaxHeight = (columnHeightObj) => {
    const columnHeightArr = Object.values(columnHeightObj)
    return Math.max(...columnHeightArr)
}
//返回列高对象中最小的高度
export const getMinHeight = (columnHeightObj) => {
    const columnHeightArr = Object.values(columnHeightObj)
    return Math.min(...columnHeightArr)
}

//返回列高对象中最小高度的所在的列

export const getMinHeightColumn = (columnHeightObj) => {
    //拿到最小的高度
    const minHeight = getMinHeight(columnHeightObj)
    return Object.keys(columnHeightObj).find((key) => {
        return columnHeightObj[key] === minHeight
    })
}
